package cn.goktech.controller;

import cn.goktech.client.SearchClient;
import cn.goktech.entity.User;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @Classname ClientController
 * @Description TODO
 * @Date 2021/2/4 0:01
 * @Created by 刘岳辉
 */
@RestController
public class ClientController {

    @Autowired
    RestTemplate restTemplate;

//    @Autowired
//    EurekaClient eurekaClient;

    @Autowired
    SearchClient searchClient;


    @GetMapping("/clientFind")
    public String clientFind(){

//        InstanceInfo info = eurekaClient.getNextServerFromEureka("SearchClient", false);
//        String url=info.getHomePageUrl();
//        return restTemplate.getForObject(url+"/find",String.class);


        //使用Robbin后直接用RestTemplate访问服务模块就行，不用调用EurekaClient了
        //SearchClient为服务模块名
//        String result = restTemplate.getForObject("http://SEARCH/find", String.class);
//        return result;

        return searchClient.find();
    }

    @GetMapping("/clientFindById/{id}")
    public User findById(@PathVariable Integer id){
        return searchClient.findById(id);
    }

    @GetMapping("/clientGetUser")
    public User getUser(@RequestParam Integer id, @RequestParam String name){
        return searchClient.getUser(id,name);
    }

    @GetMapping("/clientSave")
    public User save(User user){
        return searchClient.save(user);
    }
}
